<?php

/*	Vulpes-backend - Web-based managegent for wireless internet providers.
	Copyright (C) 2007 LinFox Serviços em Informática Ltda.

	This file is part of the Vulpes backend.

	Vulpes-backend is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Vulpes-backend is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

 	Please note that any front-end (client application) that is distributed
	along with the Vulpes back-end, such as the Vulpes front-end by LinFox,
	is subject to it's correspending licene. */

class config
{
	public $site_id = 1;
	public $sitename;
	public $dhcpd_header;
	public $iptables;
	public $language;
	public $iface;
	public $bandwith;
	private $page;
	private $db;
	
	function __construct($site_id = 1)
	{
		global $db, $page;
		$this->db =& $db;
		$this->page =& $page;
	
		$sql_config = '
			SELECT * FROM `config`
			WHERE `site_id` = "' .$site_id. '"';
		$result_config = $this->db->query($sql_config);
		$config = $result_config->fetch_object();
		$result_config->free();
		
		foreach ($config as $key => $value) {
			$this->$key = $value;
		}
	}

	function update()
	{
		if (get('sitename'))
			$this->sitename = get('sitename');
		if (get('dhcpd_header'))
			$this->dhcpd_header = stripcslashes(str_replace('\r','\n',get('dhcpd_header')));
		if (get('iptables_header'))
			$this->iptables = stripcslashes(str_replace('\r','\n',get('iptables_header')));
		if (get('iface'))
			$this->iface = get('iface');
		
		$sql_update_config = '
			UPDATE `config`
			SET `sitename` = "'.$this->sitename.'",
				`dhcpd_header` = "'.$this->dhcpd_header.'",
				`iptables` = "'.$this->iptables.'",
				`iface` = "'.$this->iface.'"
			WHERE `site_id` = "1"';
		$this->db->query($sql_update_config);
		iptables_refresh();
		$this->show_all();
	}
	
	function show_all()
	{
		$sql_config = '
			SELECT * FROM `config`
			WHERE `site_id` = "' .$this->site_id. '"';
		$result_config = $this->db->query($sql_config);
		$config = $result_config->fetch_object();
		$result_config->free();
		
		foreach ($config as $key => $value) {
			$this->$key = $value;
		}
		$this->page->content .= '<config>';
		$this->page->content .= '<sitename>'.$this->sitename.'</sitename>';
		$this->page->content .= '<dhcpd_header>'.$this->dhcpd_header.'</dhcpd_header>';
		$this->page->content .= '<iptables_header>'.$this->iptables.'</iptables_header>';
		$this->page->content .= '<iface>'.$this->iface.'</iface>';
		$this->page->content .= '</config>';
	}
}

?>